package com.kylin.module.member.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;
import org.hibernate.Query;

import com.kylin.common.exception.BusinessException;
import com.kylin.common.exception.SystemException;
import com.kylin.common.paginate.MyPage;
import com.kylin.common.paginate.PaginatedListHibernate;
import com.kylin.hiberbate.HibernateSessionFactory;
import com.kylin.module.member.business.BlogicBean;
import com.kylin.module.member.form.MemberForm;
import com.kylin.module.member.form.SeachForm;
import com.kylin.module.member.orm.MemberDAO;

public class SearchAction  extends DispatchAction{

	public ActionForward search(ActionMapping mapping, ActionForm actionForm,
			HttpServletRequest request, HttpServletResponse response) {
		System.out.println("\n.......start......Seach\n");

		String forward = "";
		ActionMessages messages = new ActionMessages();
		try {
			int pagenum=1,size=5;

			if(request.getParameter("page") !=null){
				pagenum = Integer.parseInt(request.getParameter("page"));
			}
			Query query = HibernateSessionFactory.getSession().createQuery("from Member m");
			Query queryCount = HibernateSessionFactory.getSession().createQuery("select count(*) from Member");
			PaginatedListHibernate newpage = null;
			try{
				newpage=new PaginatedListHibernate(queryCount, query,
					pagenum, size) ;
			}catch(Exception e){
				throw new BusinessException(e);
			}
			request.setAttribute("result", newpage);
			
			forward = "searchSuccee";
		} catch (BusinessException e) {
			ActionErrors errors = new ActionErrors();
			ActionError err = new ActionError(e.getMessage());
			errors.add("BusinessException", err);
			saveErrors(request, errors);
			forward = "BusinessError";
			e.printStackTrace();
		} catch (SystemException e) {
			ActionErrors errors = new ActionErrors();
			ActionError err = new ActionError(e.getMessage());
			errors.add("SystemException", err);
			saveErrors(request, errors);
			request.setAttribute("SystemException", e);
			forward = "SystemError";
			e.printStackTrace();
		} catch (Exception e) {
			ActionErrors errors = new ActionErrors();
			ActionError err = new ActionError(e.getMessage());
			errors.add("未知错误", err);
			saveErrors(request, errors);
			request.setAttribute("SystemException", e);
			forward = "SystemError";
			e.printStackTrace();
		}
		return mapping.findForward(forward);
	}
}
